<template>
  <div>
    <el-form label-position="right" :model="candidateForm" :label-width="formLabelWidth" ref="refCandidateForm" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="candidateForm.username" placeholder="请输入用户名" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="candidateForm.name" placeholder="请输入姓名" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="密码" prop="password">
            <el-input v-model="candidateForm.password" placeholder="请输入密码" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="性别" prop="gender">
            <el-select v-model="candidateForm.gender" placeholder="请选择性别">
              <el-option label="男" :value="1"></el-option>
              <el-option label="女" :value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="出生日期" prop="birthDate">
            <el-date-picker
                v-model="candidateForm.birthDate" type="date" placeholder="出生日期" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="candidateForm.contactPhone" placeholder="请输入联系电话" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="candidateForm.email" placeholder="请输入邮箱" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="省/市/区">
            <el-cascader
                v-model="provinceCityArea"
                :options="provinceCityAreaOptions"
                :props="{ expandTrigger: 'hover', value: 'code', label: 'name' }"
                @change="handleChange"></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="家庭地址" prop="address">
            <el-input v-model="candidateForm.address" placeholder="请输入家庭地址" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="form-button-container">
      <el-button type="primary" @click="onSave">保存</el-button>
      <el-button @click="onReset">重置</el-button>
    </div>
  </div>
</template>

<script>
import candidateApi from '@/api/candidateApi'
import areas from "@/locales/area.json";
export default {
  name: 'CandidateAddOfJobSeeker',
  components: {},
  mixins: [
  ],
  data () {
    return {
      candidateForm: {
        username: '',
        name: '',
        password: '',
        gender: '',
        birthDate: '',
        contactPhone: '',
        email: '',
        province: '',
        city: '',
        area: '',
        address: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ],
        birthDate: [
          { required: true, message: '请输入出生日期', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
      },
      provinceCityArea: [],
      provinceCityAreaOptions: areas.provinces
    }
  },
  created () {
    candidateApi.findById(1)
        .then(resp => {
          if (resp && resp.code === 1) {
            this.candidateForm = resp.data
            this.provinceCityArea = []
            if (this.candidateForm.province) {
              this.provinceCityArea.push(this.candidateForm.province)
            }
            if (this.candidateForm.city) {
              this.provinceCityArea.push(this.candidateForm.city)
            }
            if (this.candidateForm.area) {
              this.provinceCityArea.push(this.candidateForm.area)
            }
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
  },
  methods: {
    onReset () {
      this.candidateForm = {
        username: '',
        name: '',
        password: '',
        gender: '',
        birthDate: '',
        contactPhone: '',
        email: '',
        province: '',
        city: '',
        area: '',
        address: ''
      }
      this.provinceCityArea = []
    },
    onSave () {
      this.$refs.refCandidateForm.validate((valid) => {
        if (valid) {
          candidateApi.save(this.candidateForm)
              .then((resp) => {
                if (resp && resp.code === 1) {
                  this.$message.success('保存成功')
                } else {
                  this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
                }
              })
              .catch(error => {
                console.log(error)
                this.$message.error('保存失败')
              })
        } else {
          return false
        }
      })
    },
    handleChange(value) {
      if (value && value.length) {
        this.candidateForm.province = value[0]
      }
      if (value && value.length > 1) {
        this.candidateForm.city = value[1]
      }
      if (value && value.length > 2) {
        this.candidateForm.area = value[2]
      }
    }
  }
}
</script>

<style lang="scss">
</style>